import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
	selector: 'm-key-search',
	templateUrl: './key-search.component.html',
	styleUrls: ['./key-search.component.scss'],
})
export class KeySearchComponent implements OnInit {
	@Input() label_text = '筛选:';
	@Input() placeholder = '请输入关键词...';
	@Input() useRouteQueryParam = false;
	@Input('ngModel') value = '';
	@Output('ngModelChange') change = new EventEmitter<string>();
	constructor(private route: ActivatedRoute, private router: Router) {}

	ngOnInit() {
		if (this.useRouteQueryParam) {
			this.value = this.route.snapshot.queryParamMap.get('keyword');
		}
	}

	enter(value: string) {
		this.value = value;
		if (this.useRouteQueryParam) {
			const queryParams = value ? { keyword: value } : null;
			this.router.navigate([], { queryParams });
		}
		// alert(value)
		this.change.emit(value);
	}
}
